//
//  V4CityPopView.m
//  XinKaiFa55like
//
//  Created by 55like on 23/09/2017.
//  Copyright © 2017 55like lj. All rights reserved.
//

#import "V4CityPopView.h"
#import "RHTableView.h"

@interface V4CityPopView ()<RHTableViewDelegate,UITableViewDelegate,UITableViewDataSource>
{
    
    
    
}
@property(nonatomic,strong)RHTableView*mtableView;
@property(nonatomic,strong)RHTableView*mtableView2;
@property(nonatomic,strong)NSArray*provinceArray;
@property(nonatomic,strong)NSMutableDictionary*currentProvince;
@property(nonatomic,strong)NSMutableDictionary*currentCity;

@end
@implementation V4CityPopView
+(V4CityPopView*)showMeWithCallBackBlock:(AllcallBlock) callBackBlock{

    V4CityPopView*view=[UTILITY.currentViewController getAddValueForKey:@"V4CityPopView"];
    if (!view) {
        view=[[V4CityPopView alloc]initWithFrame:CGRectMake(0, kTopHeight+40, kScreenWidth, kScreenHeight-kTopHeight-40)];
        [UTILITY.currentViewController.view addSubview:view];
        [UTILITY.currentViewController setAddValue:view forKey:@"V4CityPopView"];
        [view addView];
    }
    view.callBackBlock=callBackBlock;
    view.hidden=NO;
    
    return view;

}

#pragma mark -  写UI
-(void)addView{
//    self.backgroundColor=RGBACOLOR(127, 127, 127, 0.5);
    UIButton *btn=[[UIButton alloc]initWithFrame:self.bounds];
    [self addSubview:btn];
    btn.backgroundColor=RGBACOLOR(127, 127, 127, 0.5);
    [btn addTarget:self action:@selector(fcloseme) forControlEvents:UIControlEventTouchUpInside];
    
    {
        RHTableView*  mtableView =[[RHTableView alloc]initWithFrame:CGRectMake(0, 0,145* kScreenWidth/320, self.frameHeight-100*kScreenHeight/570.0) style:UITableViewStylePlain];
        _mtableView=mtableView;
        mtableView.delegate = self;
        mtableView.dataSource = self;
        mtableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:mtableView];
        [mtableView reloadData];
    }
    {
        RHTableView*  mtableView =[[RHTableView alloc]initWithFrame:CGRectMake(_mtableView.frameWidth, 0, kScreenWidth-_mtableView.frameWidth, self.frameHeight-100*kScreenHeight/570.0) style:UITableViewStylePlain];
        mtableView.backgroundColor=rgb(246, 246, 246);
        _mtableView2=mtableView;
        mtableView.delegate = self;
        mtableView.dataSource = self;
        mtableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self addSubview:mtableView];
        [mtableView reloadData];
    }
    [self loadDATA];
    
    
    
}



#pragma mark - 按钮事件监听
-(void)fcloseme{
    if (self.callBackBlock) {
        self.callBackBlock(nil,201,nil);
    }
    
    [self closeme];
}

-(void)closeme{
    self.hidden=YES;
    
    
}


-(void)loadDATA{
[UTILITY getCityDataWithBlcok:^(id data, int status, NSString *msg) {
    if(status==200){
        [self revolvData:data];
    }
    
}];

}

-(void)revolvData:(NSDictionary*)dic{
    NSArray*array=[dic objectForKey:@"list"];
    if (![array isKindOfClass:[NSArray class]]) {
        return;
    }
    if (!array.count) {
        return;
    }
    NSMutableArray*proviceArray=[NSMutableArray new];
    _provinceArray=proviceArray;
    NSMutableDictionary*allState=[NSMutableDictionary dictionaryWithDictionary:@{@"name":@"全国"}];
    
    [allState setObject:@[[NSMutableDictionary dictionaryWithDictionary:@{@"name":@"全国"}]] forKey:@"list"];
    [proviceArray addObject:allState];
    for (NSDictionary*dic in array) {
        if ([[dic objectForJSONKey:@"parentid"] isEqualToString:@"0"]) {
            NSMutableDictionary*provicemdic=[NSMutableDictionary dictionaryWithDictionary:dic];
            
            [proviceArray addObject:provicemdic];
            
            NSMutableArray*provicelist=[NSMutableArray new];
            [provicemdic setObject:provicelist forKey:@"list"];
            
            [provicelist addObject:[NSMutableDictionary dictionaryWithDictionary:dic]];
            
        
            for (NSDictionary*citydic in array) {
                if ([[citydic objectForJSONKey:@"parentid"] isEqualToString:[provicemdic objectForJSONKey:@"areaid"]]) {
                    [provicelist addObject:[NSMutableDictionary dictionaryWithDictionary:citydic]];
                    
                }
            }
            
        }
    }
    _currentProvince=[_provinceArray firstObject];
    _currentCity=[[_currentProvince objectForKey:@"list"] firstObject];
    
    [self reloadtableView];

}
-(void)reloadtableView{
    [_mtableView reloadData];
    [_mtableView2 reloadData];
}
#pragma mark - 代理事件



#pragma mark -  tableview代理方法
- (NSInteger)numberOfSectionsInTableView:(RHTableView *)tableView{
  
    
    return 1;
    
}

-(NSInteger)tableView:(RHTableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (_mtableView==tableView) {
        return _provinceArray.count;
    }else if(_mtableView2==tableView){
        
      return  [[_currentProvince objectForKey:@"list"] count];
    }
    
    return 0;
}

-(CGFloat)tableView:(RHTableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0;
}
-(CGFloat)tableView:(RHTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 30;
}
-(CGFloat)tableView:(RHTableView *)tableView heightForFooterInSection:(NSInteger)section    {
    
    return 0.001;
}

-(UIView *)tableView:(RHTableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [UIView new];
    
    
}

#pragma mark  cell处理
- (UITableViewCell *)tableView:(RHTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(tableView ==_mtableView){
        //
        UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
            cell.frameWidth=tableView.frameWidth;
            cell.backgroundColor=zrgbwhiteColor;
            UILabel*lbtitle=[RHMethods lableX:10 Y:10 W:tableView.frameWidth-20 Height:12 font:12 superview:cell withColor:rgb(153,153,153) text:@"名称"];
            UIView*viewline=[RHMethods viewWithFrame:CGRectMake(10, 29.5, tableView.frameWidth-10, 0.5) backgroundcolor:rgbLineColor superView:cell];
            [cell setAddValue:lbtitle forKey:@"lbtitle"];
            [cell setAddValue:viewline forKey:@"viewline"];
        }
        UILabel*lbtitle=[cell getAddValueForKey:@"lbtitle"];
        
                NSDictionary *dic=_provinceArray[indexPath.row];
        lbtitle.text=[dic objectForJSONKey:@"name"];
        if (dic==_currentProvince) {
            cell.backgroundColor=rgb(246, 246, 246);
        }else cell.backgroundColor=zrgbwhiteColor;
        
        
        return cell;
        
     }else if(tableView ==_mtableView2){
        //
        UITableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone ;
            
            cell.frameWidth=tableView.frameWidth;
            cell.backgroundColor=rgb(246, 246, 246);
            UILabel*lbtitle=[RHMethods lableX:10 Y:10 W:tableView.frameWidth-20 Height:12 font:12 superview:cell withColor:rgb(153,153,153) text:@"名称"];
            UIImageView*imgV=[RHMethods imageviewWithFrame:CGRectMake(0, 0, 12, 8) defaultimage:@"xkf_ture" supView:cell];
            imgV.centerY=15;
            imgV.frameRX=10;
            [cell setAddValue:lbtitle forKey:@"lbtitle"];
            [cell setAddValue:imgV forKey:@"imgV"];
        }
         UILabel*lbtitle=[cell getAddValueForKey:@"lbtitle"];
         UILabel*imgV=[cell getAddValueForKey:@"imgV"];
         
         
         NSDictionary *dic=[_currentProvince objectForKey:@"list"][indexPath.row];
         lbtitle.text=[dic objectForJSONKey:@"name"];
         
         if (dic==_currentCity) {
             imgV.hidden=NO;
         }else imgV.hidden=YES;
        
        //        NSDictionary *dic=tableView.dataArray[indexPath.row];
        
        return cell;
        
    }
    return [UITableViewCell new];
}
#pragma mark  选中cell
-(void)tableView:(RHTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (tableView==_mtableView) {
         NSMutableDictionary *dic=_provinceArray[indexPath.row];
        _currentProvince=dic;
        [self reloadtableView];
    }else if(tableView ==_mtableView2){
      NSMutableDictionary *dic=[_currentProvince objectForKey:@"list"][indexPath.row];
        _currentCity=dic;
        [self reloadtableView];
        if (self.callBackBlock) {
            self.callBackBlock(_currentCity,200,nil);
            [self closeme];
        }
        
        
    }
    
}
@end

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

